<template>
  <div>
    <div>
      <div class="header flex">
      <el-input v-model="month"  placeholder="请输入:2020-12" style="width:170px" v-if="$store.getters.isShow"></el-input>
      <el-input v-model="month2"  placeholder="请输入:2020-12-02" style="width:170px" v-if="$store.getters.isShow"></el-input>
        <el-button @click="search()" type="primary" icon="el-icon-zoom-in" v-if="$store.getters.isShow">查询</el-button>
        <el-button @click="cleartIn()" type="primary" icon="el-icon-refresh-left" v-if="$store.getters.isShow">重置</el-button>
        <!-- 弹框  新增-->
        <el-dialog title="新增日报" :visible.sync="dialogFormVisible">
  <el-form :model="form">
    <el-form-item label="完成进度" :label-width="formLabelWidth">
      <el-input v-model="form.speed" autocomplete="off" placeholder="输入数字:50"></el-input>
    </el-form-item>
    <el-form-item label="日报内容" :label-width="formLabelWidth">
      <el-input   type="textarea" :rows="2"
        placeholder="请输入内容" v-model="form.content" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="工作日期" :label-width="formLabelWidth">
      <el-date-picker
      v-model="form.workDate"
      type="date"
      placeholder="选择日期"
      value-format="yyyy-MM-dd" >
    </el-date-picker>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisible = false">取 消</el-button>
    <el-button type="primary" @click="submitForm(form)">确 定</el-button>
  </div>
</el-dialog>
      </div>
      <div class="flex" style="width:100%;justify-content: end;">
        <el-button type="primary" @click="add()">新增</el-button>
        <el-button type="danger" @click="clear()">删除</el-button>

      </div>
      <!-- 下面表格 -->
      <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        border
        @selection-change="handleSelectionChange">
      <!-- 单选全选 点击按钮 -->
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column label="员工姓名" width="120">
          <template slot-scope="scope">{{ scope.row.name }}</template>
        </el-table-column>
        <el-table-column prop="name" label="完成进度" width="120">
          <template slot-scope="scope">{{ scope.row.speed }}</template>
        </el-table-column>
        <el-table-column prop="address" label="工作内容" show-overflow-tooltip>
          <template slot-scope="scope">{{ scope.row.content }}</template>
        </el-table-column>
        <el-table-column label="工作月份" width="120">
          <template slot-scope="scope">{{ scope.row.workMonth }}</template>
        </el-table-column>
        <el-table-column label="工作日期" width="120">
          <template slot-scope="scope">{{ scope.row.workDate }}</template>
        </el-table-column>
        <el-table-column prop="address" label="创建时间" show-overflow-tooltip>
          <template slot-scope="scope">{{ scope.row.createDate }}</template>
        </el-table-column>
        <el-table-column label="操作">
      <template slot-scope="scope">
        <el-button
          size="60"
          @click="handleEdit(scope.row)" type="warning">修改</el-button>
      </template>
      </el-table-column>
      </el-table>
      <el-dialog title="修改日报" :visible.sync="dialogFormVisible2">
  <el-form :model="form2">
    <el-form-item label="完成进度" :label-width="formLabelWidth">
      <el-input v-model="form2.speed" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="计划内容" :label-width="formLabelWidth">
      <el-input   type="textarea" :rows="2"
        placeholder="请输入内容" v-model="form2.content" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="工作日期" :label-width="formLabelWidth">
      <el-date-picker
      v-model="form2.workDate"
      type="date"
      placeholder="选择日期"
      value-format="yyyy-MM-dd" >
    </el-date-picker>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisible2 = false">取 消</el-button>
    <el-button type="primary" @click="submitForm2(form2)">确 定</el-button>

  </div>
</el-dialog>
      <Pagin :count="total" @pageEvent="fetchData" :pageSize="pageSize"></Pagin>
    </div>
  </div>
</template>

<script>
import {dailListAPI,addDailInsertAPI,dailUpdateAPI,dailDeleteAPI,dailSeach} from '../../api/dail'
export default {
  data() {
    return {
      month:"",
      month2 : '',
      form: {
          speed: '',
          workDate: '',
          content: ''
        },
        formLabelWidth: '120px',
      type:'danger',
      dialogFormVisible: false,
      dialogFormVisible2: false,//修改
      form2: {
        speed: '',
          workDate: '',
          content: ''
        },
      dataList:[],
      total: 0, // 列表总行数
        currentPage: 1,
        pageSize: 10,
      tableData: [],
      multipleSelection: [],
    };
  },
  created(){
    this.jia()
  },
  components: {
    Pagin: () => import('c/Pagin')
  },
  methods: {
    handleSelectionChange(val) {
      console.log(val);
      this.multipleSelection = val;
    },
    //重置
    cleartIn(){
      this.month =''
      this.month2 =''
    },
    //查询
   async search(){
    if(this.month > 12 || this.month < 1){
      this.$message.error('输入错误')
    }
    const res = await dailSeach({
      workMonth :this.month,
      workDate : this.month2
    })
    if(res.code == '1'){
        if(res.data != null){
          this.dataList = res.data
          this.total = this.dataList.length
          this.fetchData(this.currentPage)
          this.$message.success(res.msg)
        }
      }else{
          this.$message.error(res.msg)
        }
    },
   async submitForm(form) {
      const res = await addDailInsertAPI({
        speed: form.speed, //主题名
        workDate:  form.workDate,//开始时间
        content:  form.content,//内容
      })
      //请求成功后
      if(res.code == '1'){
        this.jia()
        this.$message.success('添加成功')
       }else{
        this.$message.error('添加失败')
       }
      //关闭
      this.dialogFormVisible = false
    },
    //删除
   async clear(){
      var id = []
      this.multipleSelection.forEach(item=>{
         id.push(item.id)
      })
      //请求成功后
      if(id.length > 0){
        const res = await dailDeleteAPI(id)
         //请求成功后
         if(res.code == '1'){
        this.jia()
        this.$message.success('删除成功')
       }else{
        this.$message.error('删除失败')
       }
      }
    },
    //修改
    handleEdit(row){
      // finance
         this.dialogFormVisible2 = true
         this.form2 = row
    },
    //修改确定
   async submitForm2(form){
      this.dialogFormVisible2 = false
      const res = await dailUpdateAPI({
        speed: form.speed, //主题名
        workDate:  form.workDate,//开始时间
        content:  form.content,//内容
        id:form.id
      })
      if(res.code == '1'){
        this.jia()
        this.$message.success('修改成功')
       }else{
        this.$message.error('修改失败')
       }
    },
      //新增
      add(){
      this.dialogFormVisible = true
    },
    //模拟假数据
   async jia(){
      const res = await dailListAPI()
      this.dataList = res.data
      this.total = this.dataList.length
      this.fetchData(this.currentPage)
    },
    fetchData (count) {
      var list = this.dataList
      var from = (count - 1) * this.pageSize
      const to = count * this.pageSize
      this.tableData = []
      for (; from < to; from++) {
        if (list[from]) {
          this.tableData.push(list[from])
        }
      }
    },
  },
};
</script>
<style scoped lang="scss">
.flex {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.input {
  width: 120px;
}
.header {
  width: 30%;
  // border-bottom: 1px solid #e0e0e0;
  padding: 20px 0;
}

</style>
